﻿@model Tuple<IList<Photo>, Entity>

@{
    Layout = null;
    ViewBag.Title = "Фотографии проекта";
	ViewBag.ShowedPhotosCount = 5;
}

<ul id="project_photoList" class="photoList" rel="@Url.Action("Remove", "Photo")" myAttr="@Model.Item2.Id">
	
	@for (int i = 0; i < Model.Item1.Count /*&& i < ViewBag.ShowedPhotosCount*/; i++)
	{
        var photo = Model.Item1[i];
        
		<li class="photoList_item" rel="@photo.Id" id="@("goodworxPhoto" + photo.Id)" 
            @if(i >= ViewBag.ShowedPhotosCount)
            {
                @:style="display:none;"
            }
        >
            <a href="javascript:void(0)" class="editButton editButton_remove jq_removePhotoButton" rel="@photo.Id">Удалить</a>
            <a href="@photo.OriginalSizeUrl" rel="fancybox_gallery1"><img src="@photo.XlSizeUrl" width="125" class="photoList_photo" alt="@photo.Title"/></a>
            @*<span class="photoList_photo_title">@photo.Title</span>*@
	    </li>
	}
	@if (Model.Item1.Count > ViewBag.ShowedPhotosCount)
 {
     for (int i = ViewBag.ShowedPhotosCount; i < Model.Item1.Count; i++)
	 {
			<a href="@Model.Item1[i].OriginalSizeUrl" rel="fancybox_gallery1" class="photoList_hiddenItem"></a>
	 }
 }
</ul>
@if (Model.Item1.Count > ViewBag.ShowedPhotosCount)
{
	<a href="javascript:void(0)" id="morePhotosLink">Ещё фоточек</a>
}

<script type="text/javascript">
    var numberShown = @ViewBag.ShowedPhotosCount;
    var entityType = '@Model.Item2.GetType().FullName';
    var entityId = @Model.Item2.Id;
    var removeActionUrl = '@Url.Action("Remove", "Photo")';
    $(document).ready(function () {
        $('#morePhotosLink').click(function () {
            var rootListItem = $('#project_photoList');
            var nextHidden = rootListItem.children('li').filter(':visible:last').next('li');
            if (nextHidden.length > 0) {
                rootListItem.children('li').filter(':visible:first').hide();
                nextHidden.show();
            } else {
                var listItems = rootListItem.children('li');
                listItems.hide();
                for(var i = 0; i < listItems.length && i < numberShown; i++)
                {
                    $(listItems[i]).show();
                }
            }
        });

        $(".jq_removePhotoButton").click(function(){
                var photoId = $(this).attr("rel");
                
                var data = "photoId=" + photoId + "&storyType="+ entityType +"&id=" + entityId;
                var successFunc = function () { };
                $.ajax({
                    'url': removeActionUrl,
                    'type': 'POST',
                    'data': data,
                    'success': successFunc,
                    'error': function (jqXHR, textStatus, errorThrown) {
                        alert(jqXHR + ', ' + textStatus + ', ' + errorThrown);
                    },
                    'beforeSubmit': function () {
                        //показываем крутилку
                    },
                    'complete': function () {
                        //скрыть крутилку
                    }
                });
                var photo = $(".photoList_item[rel="+photoId+"]");
                photo.siblings().filter(':hidden:first').show();
                photo.remove();
            }
        );
    });
</script>
